import { $get, $post, $put, $delete } from '@js/request'

export async function getInfoListByName (name: string, type: string) {
  const res = await $get('/api/commission/party/getByName', {
    name,
    subject_type: type
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 根据完整名称获取企业信息
export async function getDataByFullName (name: string, type: string) {
  const res = await $get('/api/commission/party/getByFullName', {
    name,
    subject_type: type
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data instanceof Array && res.data.length > 0 ? res.data[0] : null
}

// 添加当事人
export async function addPartyRequest (data: { [k: string]: any }) {
  const res = await $post('/api/commission/party', data)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return {
    ...data,
    id: res.data
  }
}

// 更新当事人
export async function updatePartyRequest (data: { [k: string]: any }) {
  const res = await $put(`/api/commission/party/${data.id}`, data)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return data
}

// 删除当事人
export async function deletePartyRequest (id: string) {
  const res = await $delete(`/api/commission/party/${id}`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return id
}
